<?php

class Activity_Table_Model_Feed extends Database_Model_Item
{

	/**
	 * @var string
	 */
	protected $_itemType = 'activity-feed';

	/**
	 * @var string
	 */
	protected $_pk = 'feed_id';

	/**
	 * @return string
	 */
	function getTitle()
	{
		return 'post';
	}

	/**
	 * @return int
	 */
	function getId()
	{
		return $this -> feed_id;
	}

	/**
	 * @return model
	 */
	function getHeadline()
	{
		$spec = Nuo::getApi('activity') -> getTypeSpec($this -> type_id);

		list($module, $method) = explode(':', $this -> type_id);

		return Nuo::getPlugin($module . '/feed') -> {$method}($this, $spec);
	}

	/**
	 * get rich contents from attachments.
	 */
	function getAttachmentRichContent()
	{
		$attachments = NULL;

		if (!$this -> attachment_count)
		{
			return '';
		}

		$groups = Nuo::getApi('core/attachment') -> groupAttachments($this);

		$xhtml = array();

		// compound by content types
		foreach ($groups as $type => $ids)
		{
			if (empty($ids))
			{
				continue;
			}

			$table = Nuo::getTable($type);

			$objs = $table -> find($ids);

			$total = $objs -> count();

			if (!$total)
			{
				continue;
			}

			$script = $this -> getRichContentScript($type);

			$xhtml[] = Nuo::getView()->render($script, array(
				'objs' => $objs,
				'feed' => $this,
				'total' => $total,
			));

		}
		return implode(PHP_EOL, $xhtml);
	}

	function getMessage()
	{
		return (string)$this -> message;
	}

	function getRichContentScript($type)
	{
		if (($pos = strpos($type, '-')) > 0)
		{
			$module = substr($type, 0, $pos);
		}
		else
		{
			$module = $type;
		}
		return $module . '/view/partial/attachment/' . $type;
	}

	function getProxy()
	{
		return $this -> getResource();
	}

	function getObject()
	{
		if ($this -> object_id)
		{
			return Nuo::getItem($this -> object_id);
		}

		return $this;
	}

}
